package net.mysoft.budgetsoft.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.pagemodel.FlowTemplateNode;
import net.mysoft.budgetsoft.system.pagemodel.YongHu;
import net.mysoft.budgetsoft.system.service.FlowTemplateNodeService;
import net.mysoft.framework.base.BaseAction;
import net.mysoft.framework.model.Json;
import net.mysoft.framework.util.ExceptionUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
//import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-07-08 17:28:18 
 */
@Action(value = "flowTemplateNodeAction", results = { 
		@Result(name = "flowTemplateNode", location = "/platform/system/flowTemplateNode.jsp"), 
		@Result(name = "flowTemplateNodeAdd", location = "/platform/system/flowTemplateNodeAdd.jsp"), 
		@Result(name = "flowTemplateNodeEdit", location = "/platform/system/flowTemplateNodeEdit.jsp"), 
		@Result(name = "flowTemplateNodeView", location = "/platform/system/flowTemplateNodeView.jsp"),
		@Result(name = "processNext", location = "/platform/processNext.jsp"),
		@Result(name = "processChart", location = "/platform/WebFlow/webflow.jsp"),
		@Result(name = "processControl", location = "/platform/processControl.jsp"),
		@Result(name = "processBack", location = "/platform/processBack.jsp")})
@Controller("flowTemplateNodeAction")
public class FlowTemplateNodeAction extends BaseAction implements ModelDriven<FlowTemplateNode>{

	private static final Logger logger = Logger.getLogger(FlowTemplateNodeAction.class);
	//@Resource
	private FlowTemplateNodeService flowTemplateNodeService;
	
	private FlowTemplateNode flowTemplateNode = new FlowTemplateNode();
	
	public FlowTemplateNodeService getFlowTemplateNodeService(){
		return this.flowTemplateNodeService;
	}
	
	public void setFlowTemplateNodeService(FlowTemplateNodeService flowTemplateNodeService){
		this.flowTemplateNodeService = flowTemplateNodeService;
	}
	
	public FlowTemplateNode getFlowTemplateNode(){
		return this.flowTemplateNode;
	}
	
	public void setFlowTemplateNode(FlowTemplateNode flowTemplateNode){
		this.flowTemplateNode = flowTemplateNode;
	}
	
	public FlowTemplateNode getModel() {
		return flowTemplateNode;
	}
	
	/**
	 * FlowTemplateNode列表页面
	 * @return
	 */
	public String flowTemplateNode() {
		return "flowTemplateNode";
	}
	
	/**
	 * FlowTemplateNode查看页面
	 * @return
	 */
	public String flowTemplateNodeView() {
		return "flowTemplateNodeView";
	}
	
	/**
	 * FlowTemplateNode添加页面
	 * @return
	 */
	public String flowTemplateNodeAdd() {
		return "flowTemplateNodeAdd";
	}
	
	/**
	 * FlowTemplateNode编辑页面
	 * @return
	 */
	public String flowTemplateNodeEdit() {
		return "flowTemplateNodeEdit";
	}
	
	/**
	 * 转下一步页面
	 * @return
	 */
	public String processNext() {
		return "processNext";
	}
	/**
	 * 控制页面
	 * @return
	 */
	public String processControl() {
		return "processControl";
	}
	/**
	 * 退回页面
	 * @return
	 */
	public String processBack() {
		return "processBack";
	}
	

	/**
	 * 流程控制——步骤查询
	 */
	public void findFlowTemplateNodeByXMRW(){
		try{
			List list = flowTemplateNodeService.findFlowTemplateNodeByXMRW(flowTemplateNode);
			super.writeJson(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下一步
	 */
	public void nextFlowTemplateNode(){
		try{
			List list = new ArrayList();
			if(flowTemplateNode.getFtc_ID()!=null&&!"".equals(flowTemplateNode.getFtc_ID())){ //还没有流程实例的时候 
				List tlist = flowTemplateNodeService.findNextFlowTemplateNode(flowTemplateNode.getXiangMuJBXXBH(),flowTemplateNode.getFtc_ID());
				for(int i=0;i<tlist.size();i++){
					FlowTemplateNode f = (FlowTemplateNode) tlist.get(i);
					logger.info("nextFlowTemplateNode:f.ftn_ID="+f.getFtn_ID()+".f.ftn_Name="+f.getFtn_Name());
					list.add(f);
				}
			}else{
				list = flowTemplateNodeService.findNextFlowTemplateNode(flowTemplateNode);
			}
			super.writeJson(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void backFlowTemplateNode(){
		try{
			List list = flowTemplateNodeService.findBackFlowTemplateNode(flowTemplateNode);
			super.writeJson(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得下一步任务的备选人
	 */
	public void candidate(){
		try{
			List<YongHu> list = flowTemplateNodeService.candidate(flowTemplateNode);
			super.writeJson(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void backUpCandidate(){
		try{
			//List<User> list = flowTemplateNodeService.backUpCandidate(flowTemplateNode);
			//super.writeJson(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void parseXMLAndSave(){
		Json j = new Json();
		try {
			flowTemplateNodeService.parseXMLAndSave(flowTemplateNode);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	/**
	 * 添加
	 */
	public void add() {
		Json j = new Json();
		try {
			flowTemplateNodeService.add(flowTemplateNode);
			j.setSuccess(true);
			j.setMsg("添加成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 修改
	 */
	public void edit() {
		Json j = new Json();
		try {
			flowTemplateNodeService.edit(flowTemplateNode);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 删除
	 */
	public void delete() {
		Json j = new Json();
		try {
			flowTemplateNodeService.delete(flowTemplateNode);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}
	
	/**
	 * 列表
	 */
	public void datagrid() {
		try {
			super.writeJson(flowTemplateNodeService.datagrid(flowTemplateNode));
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
		}
	}
}
